<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="抽奖活动名称" prop="prizeName">
        <!-- <el-input
          v-model="queryParams.prizeName"
          placeholder="请输入抽奖活动名称"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        /> -->
        <el-select
        v-model="queryParams.prizeId"
        filterable
        remote
        reserve-keyword
        placeholder="请输入要选择的奖品"
        remote-show-suffix
        :remote-method="remoteMethod"
        :loading="loading1"
        style="width: 240px"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      </el-form-item>
      <!-- <el-form-item label="抽奖活动ID" prop="prizeId">
        <el-input
          v-model="queryParams.prizeId"
          placeholder="请输入抽奖活动ID"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="抽奖人" prop="userId">
        <el-select
        v-model="queryParams.userId"
        filterable
        remote
        reserve-keyword
        placeholder="请输入用户名"
        remote-show-suffix
        :remote-method="remoteMethod2"
        :loading="loading2"
        style="width: 240px"
      >
        <el-option
          v-for="item in options2"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 查询</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['raffle:chance:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['raffle:prize:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="用户名" align="center" prop="userName" fixed />
      <el-table-column label="用户ID" align="center" prop="userId" fixed />
      <el-table-column label="活动名称" align="center" prop="prizeName" />
      <el-table-column label="活动ID" align="center" prop="prizeId" />
      <el-table-column label="每日赠送" align="center" prop="source0ChanceCount" />
      <el-table-column label="任务次数" align="center" prop="source1ChanceCount" />
      <el-table-column label="购买商品次数" align="center" prop="source2ChanceCount" />
      <el-table-column label="购买机会次数" align="center" prop="source3ChanceCount" />
      <el-table-column label="系统赠送次数" align="center" prop="source4ChanceCount" />
      <el-table-column label="总次数" align="center" prop="chanceCountAll" />
      <el-table-column label="已用次数" align="center" prop="usedCountAll" />
      <el-table-column label="剩余次数" align="center" prop="remainCount" />
      <el-table-column label="操作" align="center" min-width="160">
        <template #default="scope">
          <el-button
            type="success"
            plain
            @click="openSendForm('create', scope.row)"
          >
           赠送
          </el-button>
          <el-button
            type="danger"
            plain
            @click="openDeleteForm('delete', scope.row)"
            v-hasPermi="['raffle:chance:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ChanceForm ref="formRef" @success="getList" />
  <DeleteForm ref="deleteFormRef" @success="getList" />
  <SendForm ref="sendFormRef"  />
</template>

<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { getchancelist, selelctactivity, queryUser} from '@/api/raffle/chance'
import ChanceForm from './ChanceForm.vue'
import DeleteForm from './DeleteForm.vue'
import SendForm from './SendForm.vue'

defineOptions({ name: 'RaffleChance' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  prizeId: '',
  prizeName: '',
  userId: ''
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const options = ref([])
const options2 = ref([])
const loading1 = ref(false)
const loading2 = ref(false)

const remoteMethod = async(query: string) => {
  if (query) {
    loading1.value = true
    const res =  await selelctactivity({
      pageNo:1,
      pageSize:100,
      prizeName:query
    })
      loading1.value = false
      options.value = res.list.map((item) => {
        return {label:item.prizeName,value:item.prizeId}
      })
  } else {
    options.value = []
  }
}
const remoteMethod2 = async(query: string) => {
  if (query) {
    loading2.value = true
    const res =  await queryUser({
      pageNo:1,
      pageSize:100,
      nickname:query
    })
      loading2.value = false
      options2.value = res.list.map((item) => {
        return {label:item.nickname,value:item.id}
      })
  } else {
    options2.value = []
  }
}

/** 查询抽奖活动列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getchancelist(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryParams.prizeId=''
  // console.log(queryParams,'queryParams')
  handleQuery()
}

/** 添加操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}
/** 赠送操作 */
const sendFormRef = ref()
const openSendForm=(type: string, row?: any)=>{
  sendFormRef.value.open(type,row)
}
/** 修改操作 */
const deleteFormRef = ref()

const openDeleteForm = (type: string, row:any) => {
  deleteFormRef.value.open(type, row)
}


// /** 删除按钮操作 */
// const handleDelete = async (id: number) => {
//   try {
//     // 删除的二次确认
//     await message.delConfirm()
//     // 发起删除
//     await PostApi.deletePost(id)
//     message.success(t('common.delSuccess'))
//     // 刷新列表
//     await getList()
//   } catch {}
// }

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    // const data = await PostApi.exportPost(queryParams)
    // download.excel(data, '活动列表.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
